
var DLSS = {
    CURRENT_EVENT: -1,
    
	bundle:[],

	/**
	 * Retrieves message from the resource bundle by giving key.
	 */
    getMessage: function(key, params) {
        var message = DLSS.bundle[key];
        // Return key instead of returning of the text
        if (Ext.isEmpty(message)) {
            return key;
        }
        // Fill message text with params
        if (params != null && params.length > 0) {
            return DLSS.substitute(message, params);
        } else {
            return message;
        }
    },

	/**
	 *  Substitutes "{n}" tokens within the specified string
	 *  with the respective arguments passed in.
	 */
    substitute: function(str, rest) {
        // Replace all of the parameters in the msg string.
        var len = rest.length;
        var args;
        if (len == 1 && rest[0] instanceof Array) {
            args = rest[0];
            len = args.length;
        } else {
            args = rest;
        }
        for (var i = 0; i < len; i++) {
            str = str.replace(new RegExp("\\{" + i + "\\}", "g"), args[i]);
        }
        return str;
    },
    submitRound:function(editEvent) {
        var r = editEvent.record;
        if (r.get('name').trim()==''||
            r.get('time').trim()==''||
            r.get('rank').trim()==''||
            r.get('couplesNumber').trim()==''||
            r.get('minSuccCouples').trim()==''||
            r.get('maxSuccCouples').trim()==''||
            r.get('mode').trim()==''||
            r.get('passingScore').trim()==''||
            r.get('setsNumber').trim()==''||
            r.get('finalDanceTime').trim()==''||
            r.get('reDance').trim()=='') {
            //DLSS.markInvalid(editEvent.grid, editEvent.row, false, "Round name and letter<br>must not be empty.");
            return;
        }
        if (editEvent.grid.getStore().query('name', r.get('name'), false, false).getCount()>1) {
            DLSS.markInvalid(editEvent.grid, editEvent.row, false, "Round name must be unique.");
            return;
        }
        DLSS.markInvalid(editEvent.grid, editEvent.row,true);
        Ext.Ajax.request({
            url:'roundUpdate.htm',
            maskEl:editEvent.grid.getGridEl(),
            params:{
                id:r.get('id'),
                name:r.get('name').trim(),
                time:r.get('time').trim(),
                rank:r.get('rank').trim(),
                couplesNumber:r.get('couplesNumber').trim(),
                minSuccCouples:r.get('minSuccCouples').trim(),
                maxSuccCouples:r.get('maxSuccCouples').trim(),
                mode:r.get('mode').trim(),
                passingScore:r.get('passingScore').trim(),
                setsNumber:r.get('setsNumber').trim(),
                finalDanceTime:r.get('finalDanceTime').trim(),
                reDance:r.get('reDance').trim()
            },
            method:'POST',
            success:function(rsp) {
                var o = Ext.util.JSON.decode(rsp.responseText);
                if (o.success) {
                    DLSS.roundsStore.reload();
                } else {
                    Ext.Ajax.fireEvent('requestexception');
                }
            },
            scope:r
        });
    },
    markInvalid:function(grid, row, valid, message) {
        var r = grid.getView().getRow(row);
        if (valid) {
            Ext.get(r).removeClass('x-form-invalid');
            Ext.QuickTips.unregister(r.firstChild.firstChild.firstChild);
            var cr = r.firstChild.firstChild.firstChild;
            for (var i=0;x<cr.childNodes.length;i++) {
                Ext.QuickTips.unregister(cr.childNodes[i].firstChild);
            }
        }
        else {
            var nr = r.firstChild.firstChild.firstChild;
            for (var x=0;x<nr.childNodes.length;x++) {

                Ext.QuickTips.register({
                    target:nr.childNodes[x].firstChild,
                    text:message,
                    cls:'x-form-invalid-tip',
                    dismissDelay:5000
                });
            }
            Ext.get(r).removeClass('x-form-invalid');
            Ext.get(r).addClass('x-form-invalid');
        }
    }
    
};
